package com.hinnotek.amoeba.ui.activity.old

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.bean.old.SellPurchaseRequistionDetailBean
import com.hinnotek.amoeba.databinding.ActivitySellPurchaseRequisitionDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.AmibaViewModel
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint
import java.io.Serializable

@AndroidEntryPoint
class SellPurchaseRequisitionDetailActivity :
    BaseActivity<ActivitySellPurchaseRequisitionDetailBinding>(), View.OnClickListener {

    private var detailBean: SellPurchaseRequistionDetailBean? = null
    private var purchaseTypeTo: String? = null
    private var amibaPurchaseId = 0

    private val amibaViewModel by viewModels<AmibaViewModel>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sell_purchase_requisition_detail)
        setLeftVisible(true)
        setTitle("添加采购申请")
        setRightText("项目需料")?.setOnClickListener {
            if (detailBean != null) {
                val intent = Intent()
                intent.setClass(mContext, SellProjectRequirementsDetailActivity::class.java)
                intent.putExtra(
                    "sellPurchaseRequistionDetailBean",
                    detailBean as Serializable?
                )
                startActivity(intent)
            }
        }

    }

    override fun initVew() {
        bindingView.tvTheArrivalDate.setOnClickListener(this)
        bindingView.superButtonModify.setOnClickListener(this)
        bindingView.superButtonSubmit.setOnClickListener(this)
        bindingView.superButtonRevoke.setOnClickListener(this)
    }

    override fun setData() {
        purchaseTypeTo = intent.getStringExtra("purchase_type_to")
        val is_msg = intent.getBooleanExtra("is_msg", false)
        if (is_msg) { //是不是从消息页进入的
            if (purchaseTypeTo == "market") {

            } else if (purchaseTypeTo == "technology") {

            } else if (purchaseTypeTo == "rD") {
                bindingView.llChooseUser.visibility = View.GONE
                bindingView.llRelatedProject.visibility = View.VISIBLE
            }
        } else {
            if (purchaseTypeTo == "market") {

                bindingView.superButtonRevoke.visibility = View.VISIBLE
            } else if (purchaseTypeTo == "technology") {
                bindingView.superButtonRevoke.visibility = View.GONE
            } else if (purchaseTypeTo == "rD") {
                bindingView.superButtonRevoke.visibility = View.VISIBLE
                bindingView.llChooseUser.visibility = View.GONE
                bindingView.llRelatedProject.visibility = View.VISIBLE
            }
        }
        amibaPurchaseId = intent.getIntExtra("amibaPurchaseId", 0)
    }

    override fun onResume() {
        super.onResume()
        getAmibaPurchaseById(amibaPurchaseId)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                1 -> bindingView.tvTheArrivalDate.text = data?.getStringExtra("date")
            }
        }
    }


    override fun onClick(v: View) {
        when (v.id) {
            R.id.tv_the_arrival_date -> IntentUtils.startActivityForResult(
                this,
                MaterialDesignCalendarActivity::class.java,
                null,
                1
            )
            R.id.superButton_modify -> {
                val bundle = Bundle()
                bundle.putInt(
                    "amibaPurchaseId",
                    detailBean?.amibaPurchaseId?:0
                )
                bundle.putString("purchase_type_to", purchaseTypeTo)
                IntentUtils.startActivity(
                    mContext,
                    SellPurchaseRequisitionModifyActivity::class.java,
                    bundle
                )
            }
            R.id.superButton_submit -> resubmitAmibaPurchase(amibaPurchaseId)
            R.id.superButton_revoke -> backAmibaPurchase(amibaPurchaseId)
        }
    }

    /**
     * app端通过id查询单个详情接口
     */
    fun getAmibaPurchaseById(amibaPurchaseId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        amibaViewModel.getAmibaPurchaseById(amibaPurchaseId)
            .observe(this, object : ObserverState<SellPurchaseRequistionDetailBean>() {
                override fun onDataChange(data: SellPurchaseRequistionDetailBean) {
                    if (purchaseTypeTo == "market") {
                        bindingView.tvChooseUser.text = data.customerName
                        bindingView.tvRelatedProject.text = data.projectName
                    } else if (purchaseTypeTo == "technology") {
                        bindingView.tvChooseUser.text = data.customerName
                        bindingView.tvRelatedProject.text = data.projectName
                    } else if (purchaseTypeTo == "rD") {
                        bindingView.llChooseUser.visibility = View.GONE
                        bindingView.llRelatedProject.visibility = View.VISIBLE
                        bindingView.tvRelatedProject.text = data.provideProjectName
                    }
                    bindingView.tvDocumentNo.text = data.documentNum
                    bindingView.tvMode.text = if (data.applicationMethod == 1) "首次" else "变更"
                    bindingView.tvDeliveryDate.text = data.deliveryDate
                    bindingView.tvProjectOverview.text = data.projectOverview
                    bindingView.tvSkillsRequirement.text = data.technicalRequirement
                    bindingView.tvSellApplicant.text = data.createUser
                    bindingView.tvApplicantStatus.text = "已申请"
                    bindingView.tvApplicantStatus.setTextColor(getColor21(R.color.black))
                    bindingView.tvSellApprover.text = data.approvalUser
                    if (data.approvalState == 1) {
                        bindingView.tvApproverStatus.text = "待审批"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.black))
                    } else if (data.approvalState == 2) {
                        bindingView.tvApproverStatus.text = "已同意"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_btn_green_normal_color))
                    } else if (data.approvalState == 3) {
                        bindingView.tvApproverStatus.text = "已拒绝"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_config_color_red))
                        bindingView.llReject.visibility = View.VISIBLE
                        bindingView.tvReject.text = data.refusalReasons
                    } else if (data.approvalState == 4) {
                        bindingView.tvApplicantStatus.text = "已撤回"
                        bindingView.tvApplicantStatus.setTextColor(getColor21(R.color.xui_config_color_red))
                    }
                    bindingView.tvSellCc.text = "宁创"
                    if (data.approvalState == 1) {
                        bindingView.superButtonRevoke.visibility = View.VISIBLE
                        bindingView.superButtonModify.visibility = View.GONE
                        bindingView.superButtonSubmit.visibility = View.GONE
                    } else if (data.approvalState == 3 ||
                        data.approvalState == 4
                    ) {
                        bindingView.superButtonModify.visibility = View.VISIBLE
                        bindingView.superButtonSubmit.visibility = View.VISIBLE
                        bindingView.superButtonRevoke.visibility = View.GONE
                    }
                    super.onDataChange(data)
                }
                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }

    /**
     * app端撤回采购申请单接口
     */
    private fun backAmibaPurchase(amibaPurchaseId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        amibaViewModel.backAmibaPurchase(amibaPurchaseId)
            .observe(this, object : ObserverState<BaseBean>() {
                override fun onDataChange(data: BaseBean) {
                    customDialog.dismiss()
                    getAmibaPurchaseById(amibaPurchaseId)
                    super.onDataChange(data)
                }
                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }

    /**
     * app端重新提交采购申请单接口
     */
    private fun resubmitAmibaPurchase(amibaPurchaseId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        amibaViewModel.resubmitAmibaPurchase(amibaPurchaseId)
            .observe(this, object : ObserverState<BaseBean>() {
                override fun onDataChange(data: BaseBean) {
                    customDialog.dismiss()
                    finishActivity()

                    super.onDataChange(data)
                }
                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })
    }

}